package web;

import Utils.CookieUtil;
import Utils.JSONUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import beans.LoginStatus;

import java.io.IOException;

@WebServlet(value = "/user/logout")
public class logoutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        Cookie[] cookies = request.getCookies();
        Cookie autoLogin = CookieUtil.getCookie(cookies, "autoLogin");
        if (session!=null) {
            session.invalidate();
        }
        if (autoLogin!=null) {
            autoLogin.setMaxAge(0);
            response.addCookie(autoLogin);
        }
        response.getWriter().write(JSONUtils.result(LoginStatus.LOGIN_OUT,null));
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
